package org.zebra.beansearch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.zebra.common.core.BaseResponseBodyAdviceChain;
import org.zebra.common.core.GeneralYmlPropertySourceFactory;

/**
 * @author zhanghongbin
 */
@AutoConfiguration
@PropertySource(value = "classpath:beansearch-springboot.yml", factory = GeneralYmlPropertySourceFactory.class)
public class BeanSearchConfig {

    public BeanSearchConfig(@Autowired(required = false) BaseResponseBodyAdviceChain baseResponseBodyAdviceChain) {
        if (baseResponseBodyAdviceChain != null) {
            BaseResponseBodyAdviceChain temp = baseResponseBodyAdviceChain.getBaseResponseBodyAdviceChain();
            BaseResponseBodyAdviceChain preTemp = baseResponseBodyAdviceChain;
            while (true) {
                if (temp == null) {
                    preTemp.setBaseResponseBodyAdvice(new BeanSearchBaseResponseBodyAdviceChain());
                    break;
                }
                preTemp = temp;
                temp = temp.getBaseResponseBodyAdviceChain();
            }
        }
    }

    @ConditionalOnMissingBean
    @Bean
    public LogicDeleteParamFilter logicDeleteParamFilter() {
        return new LogicDeleteParamFilter();
    }
}
